package com.superhero.website.controller;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.superhero.common.exception.BusinessException;
import com.superhero.common.pojo.ApiResult;
import com.superhero.common.pojo.PageInput;
import com.superhero.common.pojo.entity.AuthUser;
import com.superhero.common.pojo.vo.PageVo;
import com.superhero.common.pojo.vo.ValidatedGroup.Insert;
import com.superhero.common.pojo.vo.ValidatedGroup.Update;
import com.superhero.common.pojo.vo.website.InspectorInput;
import com.superhero.common.pojo.vo.website.InspectorListVo;
import com.superhero.common.utils.ExcelUtils;
import com.superhero.website.service.impl.InspectorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;


@Api(tags = {"后台管理系统"})
@RestController
@RequestMapping("/website/inspector")
public class InspectorController {

    @Autowired
    InspectorServiceImpl inspectorService;

    @ApiOperation(value = "新建检查人员")
    @PostMapping("")
    private ApiResult<String> append(@Validated({Insert.class}) InspectorInput input) {
        if (!IdcardUtil.isValidCard(input.getIdCard())) {
            throw new BusinessException("身份证输入不合法");
        }
        AuthUser append = inspectorService.append(input);
        return ApiResult.success();
    }

    @ApiOperation(value = "查询检查人员信息详情")
    @GetMapping("/get")
    private ApiResult<InspectorListVo> getDetail(@NotNull Integer id) {
        InspectorListVo vo = inspectorService.getDetail(id);
        return ApiResult.success(vo);
    }

    @ApiOperation(value = "列表显示检查人员（分页）")
    @GetMapping("")
    private ApiResult<PageVo> showList(PageInput queryInput, String keyword) {
        return ApiResult.success(inspectorService.showList(queryInput, keyword));
    }

    @ApiOperation(value = "导出检查人员列表", notes = "没有数据时，返回null")
    @GetMapping("/export")
    private void exportList(String keyword, HttpServletResponse response) {
        ExcelWriter excelWriter = inspectorService.getExcelWriter(keyword);
        if (null != excelWriter) {
            // 进行 excel 写出操作。
            try {
                ExcelUtils.responseExcel("检查人员信息", response, excelWriter);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "修改检查人员")
    @PutMapping("")
    private ApiResult<String> update(@Validated({Update.class}) InspectorInput input) {
        if (!IdcardUtil.isValidCard(input.getIdCard())) {
            throw new BusinessException("身份证输入不合法");
        }
        AuthUser update = inspectorService.update(input);
        return ApiResult.success();
    }

    @ApiOperation(value = "删除检查人员")
    @DeleteMapping("")
    private ApiResult<String> delete(@NotNull Integer id) {
        inspectorService.delete(id);
        return ApiResult.success();
    }

}
